﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LiUTrading.Models
{
    public class AdRepository
    {
        private LiuTradingDBEntities entities = new LiuTradingDBEntities();

        //Query Methods
        public IQueryable<Ad> FindAllAds(){
            
            return from ad in entities.Ads orderby ad.DateTime descending select ad;   
        }
        public IQueryable<Ad> FindAllBookAds()
        {
            return from ad in entities.Ads
                   where ad.Category == "Books"
                   orderby ad.DateTime
                   select ad;
        }
        public IQueryable<Ad> FindUsersAds(int id)
        {
            return from ad in entities.Ads
                   where ad.UserId == id
                   orderby ad.DateTime
                   select ad;
        }
        public Ad GetAd(int id){
            return entities.Ads.FirstOrDefault(a => a.AdId == id);
        }

        //Insert/delete
        public void Add(Ad ad)
        {
            entities.Ads.AddObject(ad);
        }

        public void Delete(Ad ad)
        {
            entities.Ads.DeleteObject(ad);
        }

        public void Save()
        {
            entities.SaveChanges();
        }
    }
}